import {defineStore} from 'pinia'
import {piniaStore} from '@/store'
import api from '@/api'

import {useRouteStore} from './route'
import {useMenuStore} from './menu'
import {ElMessage} from 'element-plus'

export const useUserStore = defineStore(
    // 唯一ID
    'user',
    {
        state: () => ({
            account: localStorage.account || '',
            token: localStorage.token || '',
            failure_time: localStorage.failure_time || '',
            permissions: []
        }),
        getters: {
            isLogin: state => {
                let retn = false
                if (state.token) {
                    console.log("token是"+state.token)
                    console.log("当前时间是"+new Date().getTime())
                    console.log("过期时间"+state.failure_time)
                    if (new Date().getTime()-state.failure_time<200000) {
                        retn = true
                    }
                }
                return retn
            }
        },
        actions: {
            login(data) {
                return new Promise((resolve, reject) => {
                    // 通过 后端登录接口
                    api.post('loginByUsername', data).then(res => {
                        if (res.code === '0') {
                            localStorage.setItem('account', data.account)
                            localStorage.setItem('password', data.password)
                            localStorage.setItem('token', res.data.token)
                            localStorage.setItem('head_picture', res.data.user.head_picture)
                            localStorage.setItem('failure_time', Math.ceil(new Date().getTime()) + 24 * 60 * 60)
                            this.account = data.account
                            this.token = res.data.token
                            this.permissions = res.data.permissions
                            this.failure_time = new Date().getTime()
                            resolve()
                        } else {
                            let message = '账号或密码错误'
                            ElMessage({
                                message,
                                type: 'error'
                            })
                            resolve()
                        }

                    }).catch(error => {
                        reject(error)
                    })
                })
            },
            logout() {
                return new Promise(resolve => {
                    const routeStore = useRouteStore()
                    const menuStore = useMenuStore()
                    localStorage.removeItem('account')
                    localStorage.removeItem('token')
                    localStorage.removeItem('failure_time')
                    this.account = ''
                    this.token = ''
                    this.failure_time = ''
                    routeStore.removeRoutes()
                    menuStore.setActived(0)
                    resolve()
                })
            },
            // 获取我的权限
            getPermissions() {
                return new Promise(resolve => {
                    api.get('user/selectPermissions?account=' + localStorage.getItem("account"),
                    ).then(res => {
                        this.permissions = res.data.permissions
                        console.log("权限是" + res.data.permissions)
                        resolve(res.data.permissions)
                    })
                    // console.log(+this.permissions)
                    // resolve(this.permissions)
                })
            },
            editPassword(data) {
                return new Promise(resolve => {
                    api.post('user/updatePassWod', {
                        account: this.account,
                        oldpassword: data.oldpassword,
                        newpassword: data.newpassword,
                        newpasswordAgain: data.newpasswordAgain,
                    }).then(() => {
                        resolve()
                    })
                })
            }
        }
    }
)

export function useUserOutsideStore() {
    return useUserStore(piniaStore)
}
